package com.zhny.aiot.control.cabinet.plugin.factory.imp;

import com.zhny.aiot.control.cabinet.plugin.ControlCabinetConst;
import com.zhny.aiot.control.cabinet.plugin.factory.IControlCabinetFactory;
import com.zhny.aiot.control.cabinet.plugin.model.ControlCabinetStoreEntry;
import com.zhny.aiot.plugin.modbus.device.ModemDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import io.netty.buffer.ByteBuf;

public class QueryStatusFactoryImpl implements IControlCabinetFactory {
    @Override
    public int getCode() {
        return ControlCabinetConst.QUERY;
    }

    @Override
    public ControlCabinetStoreEntry getStoreEntry(ModemResponseMessage modemResponseMessage, ModemDevice device) {
        int code = modemResponseMessage.getFunction();
        ByteBuf buf = modemResponseMessage.getData();
        int len = modemResponseMessage.getSize();
        ControlCabinetStoreEntry storeEntry = new ControlCabinetStoreEntry();
        if(code == getCode()){
            int value = buf.readByte() & 0xff;
            if(value == 5){
                storeEntry.setValve1status(true);
                storeEntry.setValve2status(true);
            }
            else if(value == 6){
                storeEntry.setValve1status(false);
                storeEntry.setValve2status(true);
            }
            if(value == 9){
                storeEntry.setValve1status(true);
                storeEntry.setValve2status(false);
            }
            if(value == 10){
                storeEntry.setValve1status(false);
                storeEntry.setValve2status(false);
            }
            device.saveReal(storeEntry);
            return storeEntry;
        }
        return null;
    }
}
